import time
import unittest

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service


class MyTestCaseFireFox(unittest.TestCase):
    driver = None

    @classmethod
    def setUpClass(cls) -> None:
        # 注册 驱动打开的位置
        service = Service(executable_path="../../dirver_exe/geckodriver.exe")
        # 打开浏览器  driver 代表 浏览器对象
        cls.driver = webdriver.Firefox(service=service)

    @unittest.skipIf(condition=2 < 3, reason="条件成立时跳过")
    def test_by(self):
        """
        测试 的八大元素定位中的  前两个 LINK_TEXT   PARTIAL_LINK_TEXT
        :return:
        """
        self.driver.get("http://192.168.0.152:8080/testhtml/exam/index.html")

        # 1、通过 精确 文本值定位的方式来搜索
        next = self.driver.find_element(By.LINK_TEXT, "继续提供房源信息")
        next.click()

        self.driver.back()
        time.sleep(3)
        # 2、通过模糊搜索的方式 来进行元素定位

        next = self.driver.find_element(By.PARTIAL_LINK_TEXT, "继续提")
        next.click()

    def test_by_2(self):
        """
        测试 的八大元素定位中的  Xpath   CssSelector  name   ClassName   id
        :return:
        """
        self.driver.get("http://192.168.0.152:8080/testhtml/exam/House.html")
        text = self.driver.find_element(By.XPATH, "//input[@type='text']")
        text.clear()
        text.send_keys('hanamaki')

        time.sleep(2)
        text = self.driver.find_element(By.CSS_SELECTOR, "input.uname")
        text.clear()
        text.send_keys('andy')

        time.sleep(2)
        text = self.driver.find_element(By.NAME, "username")
        text.clear()
        text.send_keys('jojo')

        time.sleep(2)
        text = self.driver.find_element(By.CLASS_NAME, "uname")
        text.clear()
        text.send_keys('rose')

        email = self.driver.find_element(By.ID, "email")
        email.clear()
        email.send_keys('123@qq.com')

    def test_by_3(self):
        """
        测试 的八大元素定位中的  TagName
        :return:
        """
        self.driver.get("http://192.168.0.152:8080/testhtml/exam/index.html")
        text = self.driver.find_element(By.TAG_NAME, "a")
        text.click()

    @classmethod
    def tearDownClass(cls) -> None:
        # cls.driver.close()  # 只会关闭一个标签页
        # cls.driver.quit()  # 直接退出浏览器 并且关闭所有的标签页
        pass


if __name__ == '__main__':
    unittest.main()
